    package com.alb.core.response;

    import cn.hutool.json.JSON;
    import com.alb.common.enums.ResponseCode;
    import com.alb.common.utils.JSONUtil;
    import com.fasterxml.jackson.databind.node.ObjectNode;
    import io.netty.handler.codec.http.*;
    import lombok.Data;
    import org.asynchttpclient.Response;
    import org.asynchttpclient.util.HttpConstants;

    /**
     * 网关回复消息对象
     * @author MattS
     * @date 2024/04/13
     */
    @Data
    public class GatewayResponse {

        //默认响应头
        private HttpHeaders responseHeaders=new DefaultHttpHeaders();

        //额外响应头
        private HttpHeaders extraResponseHeaders=new DefaultHttpHeaders();

        //响应体的内容
        private String content;

        //响应状态码
        private HttpResponseStatus httpResponseStatus;

        //异步返回对象
        private Response futureReponse;

        public GatewayResponse() {
        }


        /**
         * 设置响应头信息
         */
        public void putHeader(CharSequence key,CharSequence val){
            responseHeaders.add(key,val);
        }

        /**
         * 构建异步的响应对象
         * @param futureResponse
         * @return {@link GatewayResponse}
         */
        public static GatewayResponse buildGatewayResponse(Response futureResponse){
            GatewayResponse response = new GatewayResponse();
            response.setFutureReponse(futureResponse);
            response.setHttpResponseStatus(HttpResponseStatus.valueOf(futureResponse.getStatusCode()));
            return response;
        }

        /**
         * 失败时返回的一个Json类型的响应信息
         * @param code
         * @param args
         * @return {@link GatewayResponse}
         */
        public static GatewayResponse buildGatewayResponse(ResponseCode code,Object ...args){
            ObjectNode objectNode = JSONUtil.createObjectNode();
            objectNode.put(JSONUtil.STATUS,code.getStatus().code());
            objectNode.put(JSONUtil.CODE,code.getCode());
            objectNode.put(JSONUtil.MESSAGE,code.getMessage());

            GatewayResponse response = new GatewayResponse();
            response.setHttpResponseStatus(code.getStatus());
            response.putHeader(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON+";charset=utf-8");
            response.setContent(JSONUtil.toJSONString(objectNode));
            return response;
        }

        /**
         * 成功时返回的一个Json类型的响应信息
         * @param data
         * @return {@link GatewayResponse}
         */
        public static GatewayResponse buildGatewayResponse(Object data){
            ObjectNode objectNode = JSONUtil.createObjectNode();
            objectNode.put(JSONUtil.STATUS,ResponseCode.SUCCESS.getStatus().code());
            objectNode.put(JSONUtil.CODE,ResponseCode.SUCCESS.getCode());
            objectNode.putPOJO(JSONUtil.DATA,data);

            GatewayResponse response = new GatewayResponse();
            response.setHttpResponseStatus(ResponseCode.SUCCESS.getStatus());
            response.putHeader(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON+";charset=utf-8");
            response.setContent(JSONUtil.toJSONString(objectNode));
            return response;
        }



    }
